package com.example.mockitostudy.controller;

import com.example.mockitostudy.dao.UserMapper;
import com.example.mockitostudy.entity.User;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    private UserMapper userMapper;

    public UserController(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @RequestMapping("/login")
    public String getUser(HttpServletRequest httpServletRequest) {
        final String userName = httpServletRequest.getParameter("username");
        final String password = httpServletRequest.getParameter("password");
        try {
            User user = userMapper.findUser(userName, password);
            if (ObjectUtils.isEmpty(user)) {
                return "/login.html";
            } else {
                return "/index.html";
            }
        } catch (Exception e) {
            return "/505.html";
        }
    }
}
